<template>
  <div class="rich-editor-wrapper">
    <!-- 编辑区域 -->
    <div class="editor-area">
      <Toolbar :editor="editor" :defaultConfig="toolbarConfig" mode="default" />
      <Editor
        v-model="html"
        :defaultConfig="editorConfig"
        mode="default"
        style="height: 300px; overflow-y: auto"
        @onCreated="handleCreated"
      />
    </div>
  </div>
</template>

<script>
import '@wangeditor/editor/dist/css/style.css'
import { Editor, Toolbar } from '@wangeditor/editor-for-vue'

export default {
  components: { Editor, Toolbar },
  props: {
    content: {
      type: String,
      default: ''
    }
  },
  data() {
    return {
      editor: null,
      html: this.content,
      toolbarConfig: {},
      editorConfig: {
        placeholder: '请输入内容...',
        MENU_CONF: {
          uploadImage: {
            base64LimitSize: 1 * 1024 * 1024 // 1MB
          },
          insertVideo: {
            parseVideoSrc(src) {
              return src
            }
          }
        }
      }
    }
  },
  watch: {
    content(newVal) {
      if (newVal !== this.html) {
        this.html = newVal
      }
    },
    html(newVal) {
      this.$emit('update-content', newVal)
    }
  },
  methods: {
    handleCreated(editor) {
      this.editor = editor
    }
  },
  beforeDestroy() {
    if (this.editor) this.editor.destroy()
  }
}
</script>

<style scoped>
.rich-editor-wrapper {
  border: 1px solid #ddd;
  border-radius: 4px;
}
.editor-area {
  padding: 10px;
}
</style>
